package com.choosefine.it.team.controller;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.team.model.vo.TeamProjectCollectionVO;
import com.choosefine.it.team.service.TeamProjectCollectionService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 11.15 dzq 班组收藏工人
 * 
 * @author Administrator
 * 
 */
@SuppressWarnings("rawtypes")
@RestController
@RequestMapping("/team/teamProjectCollection")
@Api(value = "班组项目收藏相关", description = "潘钱钦")
public class TeamProjectCollectionController extends BaseController {

	@Autowired
	private TeamProjectCollectionService teamProjectCollectionService;

	@Autowired
	CustomRestTemplate restTemplate;

	@Autowired
	ObjectMapper jsonObjectMapper;

	Logger logger = LoggerFactory.getLogger(TeamProjectCollectionController.class);

	@Value("#{yaml.get('basedata.teamType.getTeamTypeById')}")
	String teamTypeURL = null;

	/**
	 * yuxin 3.1
	 */
	@ApiOperation(value = "班组收藏项目-收藏")
	@RequestMapping(method = RequestMethod.POST,value = "addTeamCollectionProject/{userCode}/{uuid}/{publishProjectId}")
    @ApiImplicitParams({
    		@ApiImplicitParam(name = "userCode",value = "班组的code",required = true,dataType = "String",paramType = "path"),
			@ApiImplicitParam(name = "uuid",value = "uuid",required = true,dataType = "String",paramType = "path"),
			@ApiImplicitParam(name = "publishProjectId",value = "发布的id",required = true,dataType = "Integer",paramType = "path"),
	})
	public String addTeamCollectionProject(@PathVariable String userCode,
					  @PathVariable String uuid,
					  @PathVariable int publishProjectId) throws Exception {
		String result = null;
		Map map=new HashMap();
		map.put("userCode",userCode);
		map.put("uuid",uuid);
		map.put("publishId",publishProjectId);
		map.put("type", CommonConstant.TEAM_COLLECT_MANAGER);
		map.put("ctime",System.currentTimeMillis());
		int num = teamProjectCollectionService.insertSelective(map);
		if (num> 0) {
			result = responseSuccess(num);
		} else {
			result = responseFail(ResultCode.MSG_CREATE_ERROR);
		}
		return result;
	}

	@ApiOperation(value = "获取班组收藏的项目列表", notes = "根据userCode查询班组收藏的项目列表", response = TeamProjectCollectionVO.class)
	@ApiImplicitParams({@ApiImplicitParam(name = "userCode", value = "用户的编号", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "uuid", value = "用户的uuid", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "pageNum", value = "起始页", required = false, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", required = false, dataType = "int", paramType = "query")})
	@RequestMapping(value = "/getTeamProjectCollectionList/{userCode}/{uuid}", method = RequestMethod.GET)
	public String getTeamProjectCollectionList(@PathVariable String userCode, @PathVariable String uuid, Integer pageNum, Integer pageSize)
			throws JsonProcessingException {
		String result = null;
		Map<String ,Object> params = new HashMap<String,Object>();
		params.put("userCode",userCode);
		params.put("uuid",uuid);
		params.put("type", CommonConstant.TEAM_COLLECT_MANAGER);//班组收藏项目对应的类型id
		PageDTO pageDTO= new PageDTO();
		pageDTO.setPageSize(pageSize);
		pageDTO.setPageNum(pageNum);
			Page<TeamProjectCollectionVO> list = teamProjectCollectionService.getTeamProjectCollectionList(params,pageDTO);
			if (list.size() > 0) {
				result = responseSuccess(list);
			} else {
				result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
			}
		return result;
	}

	@ApiOperation(value = "班组取消收藏某个收藏的项目", notes = "根据userCode和需要分页的参数来查询班组收藏的项目列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userCode",value = "班组的code",required = true,dataType = "String",paramType = "path"),
			@ApiImplicitParam(name = "uuid",value = "uuid",required = true,dataType = "String",paramType = "path"),
			@ApiImplicitParam(name = "publishProjectId",value = "发布的id",required = true,dataType = "Integer",paramType = "path"),
	})
	@RequestMapping(value = "/cancleProjectCollect/{userCode}/{uuid}/{publishProjectId}", method = RequestMethod.PUT)
	public String cancleProjectCollect(@PathVariable String userCode,
									   @PathVariable String uuid,
									   @PathVariable int publishProjectId) {
		String result = null;
		Map map=new HashMap();
		map.put("userCode",userCode);
		map.put("uuid",uuid);
		map.put("publishId",publishProjectId);
		map.put("type", CommonConstant.TEAM_COLLECT_MANAGER);
		map.put("utime",System.currentTimeMillis());
			int num = teamProjectCollectionService.deleteProjectCollect(map);
			if (num > 0) {
				result = responseSuccess(num);
			} else {
				result = responseFail(ResultCode.FAILED, ResultCode.MSG_UPDATE_FAILD);
			}

		return result;
	}
}
